﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class MainController : Controller
    {
        public IAdminUserService AdminUserService { get; set; }
        // GET: Main
        public ActionResult Index()
        {
            long? adminUserId = AdminHelper.GetUserId(HttpContext);

            if (adminUserId == null)
            {
                return RedirectToAction("Index", "AdminLogin");
            }

            var adminUser = AdminUserService.GetById((long)adminUserId);


            return View(adminUser);
        }



        [HttpPost]
        public ActionResult Logout()
        {
            Session.Abandon();
            return Json(new AjaxResult { Status = "ok", Data = "/AdminLogin/Index" });
        }

        [HttpGet]
        [CheckPermission("AdminUser.ChangePwd")]
        public ActionResult ChangePwd()
        {
            long id = Convert.ToInt64(Session["ADMINID"]);

            var model = AdminUserService.GetById(id);
            return View(model);
        }

        [HttpPost]
        [CheckPermission("AdminUser.ChangePwd")]
        public ActionResult ChangePwd(ChangePwd model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            AdminUserService.UpdatePwd(model.Id, model.NewPassWord);
            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("AdminUser.Info")]
        public ActionResult AdminInfo()
        {
            long id = Convert.ToInt64(Session["ADMINID"]);

            var model = AdminUserService.GetById(id);
            return View(model);
        }
    }
}